<?php

use Biz\model\brandActivity\BlindBoxCueCard as mBlindBoxCueCard;
use Biz\model\brandActivity\BlindBoxCueCardRecord as mBlindBoxCueCardRecord;
use Biz\service\brand\BlindBox;
use Biz\cache\brand\BrandShowActivity;

/**
 * 使用提示卡
 */
class UseCueCardAction extends ActionPlugin
{
    const USER_LEVEL = self::LEVEL_REGISTER;

    public function main()
    {
        if (!BrandShowActivity::isLock($this->uid . '-blind-box',2)) {
            $boxId = $this->getRequest()->getPost('box_id', 0);
            $cardId = $this->getRequest()->getPost('card_id', 0);
            $boxMap = BlindBox::getBlindBox($this->uid);
            $allBoxId = array_keys($boxMap);
            $canOpenBoxNum = BlindBox::canOpenBox($this->uid);
            if ($canOpenBoxNum <= 0) {
                throw new \Exception('今天不能再用提示卡了哦～');
            }
            if (!$boxId || !$cardId || !in_array($boxId, $allBoxId)) {
                throw new \Exception('参数错误');
            }
            $cardInfo = mBlindBoxCueCard::getByWhere(['id' => $cardId, 'expire_time[>]' => date('Y-m-d H:i:s')]);
            if (empty($cardInfo)) {
                throw new \Exception('不存在或则已过期的提示卡');
            }
            if ($cardInfo['card_state'] == mBlindBoxCueCard::CARD_STATE_USED) {
                throw new \Exception('当前提示卡已经使用过，不能重复使用');
            }
            $start = date('Y-m-d');
            $end = date('Y-m-d H:i:s', strtotime($start) + 86399);
            $todayUseList = mBlindBoxCueCardRecord::selectByWhere(['uid' => $this->uid, 'box_id' => $boxId, 'addtime[>=]' => $start, 'addtime[<=]' => $end]);
            $todayUseCount = $todayUseList ? count($todayUseList) : 0;
            if ($todayUseCount >= BlindBox::USER_EVERY_DAY_USE_CUE_CARD) {
                throw new \Exception('每个盒子最多使用' . BlindBox::USER_EVERY_DAY_USE_CUE_CARD . '次提示卡哦');
            }
            $excludedPrizeIds = $todayUseList ? array_column($todayUseList, 'prize_id') : [];
            $prizeList = BlindBox::getPrizeList($this->uid);
            unset($prizeList[BlindBox::EMPTY_BOX_PRIZE_ID]);
            foreach ($excludedPrizeIds as $excludedPrizeId) {
                if (isset($prizeList[$excludedPrizeId])) {
                    unset($prizeList[$excludedPrizeId]);
                }
            }
            $prizeId = array_rand($prizeList, 1);
            $prizeInfo = $prizeList[$prizeId];
            $prizeInfo['is_exclude'] = 1;
            $result = $prizeInfo;
            if ($result) {
                //添加使用记录
                mBlindBoxCueCardRecord::insertData(['uid' => $this->uid, 'cue_card_id' => $cardId, 'card_type' => $cardInfo['card_type'],
                    'box_id' => $boxId, 'activity_id' => $prizeInfo['activity_id'], 'prize_id' => $prizeInfo['prize_id'], 'prize_type' => $prizeInfo['prize_type']]);
                mBlindBoxCueCard::updateData(['card_state' => mBlindBoxCueCard::CARD_STATE_USED], ['id' => $cardId]);
            }
            $this->setSuccess($result);
        } else {
            throw new \Exception('你的操作太频繁');
        }
    }
}